package DAY1209.Demo5;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.List;

/**
 * @Author:LiRegin
 * @Date: 2019/12/9 17:50:57
 */
public class Test {
    public static void main(String[] args) throws DocumentException {
        SAXReader reader = new SAXReader();
        Document dom = reader.read(new File("C:\\XML\\src\\DAY1209\\Demo5\\book.xml"));
        Element root = dom.getRootElement();
        List<Element> servletList = root.elements("book");
        for (Element servlet : servletList) {
            if (Integer.parseInt(servlet.attributeValue("id")) >= 2) {
                System.out.println("book id:" + Integer.parseInt(servlet.attributeValue("id")));
                List<Element> elements = servlet.elements();
                for (Element element : elements) {
                    System.out.println(element.getName() + ":" + element.getText());
                }
            }
        }
        System.out.println("=============================================================");
        for (Element servlet : servletList) {
            List<Element> elements = servlet.elements();
            if (elements.get(0).getText().contains("Java")) {
                System.out.println("book id:" + Integer.parseInt(servlet.attributeValue("id")));
                for (Element element : elements) {
                    System.out.println(element.getName() + ":" + element.getText());
                }
            }
        }
        System.out.println("=============================================================");
        for (Element servlet : servletList) {
            List<Element> elements = servlet.elements();
            if (Integer.parseInt(elements.get(2).getText())>50) {
                System.out.println("book id:" + Integer.parseInt(servlet.attributeValue("id")));
                for (Element element : elements) {
                    System.out.println(element.getName() + ":" + element.getText());
                }
            }
        }
    }
}
